<template>
  <div class="pop-dialog-container">
    <div class="pop-dialog-main">
      <el-form label-position="right" :model="employeeForm" :label-width="formLabelWidth" ref="refEmployeeForm" :rules="rules">
        <el-row>
          <el-col :span="11">
            <el-form-item label="企业名称" prop="enterpriseName">
              <el-input v-model="employeeForm.enterpriseName" placeholder="请选择企业名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findEnterprise"></i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="工号" prop="employeeNumber">
              <el-input v-model="employeeForm.employeeNumber" placeholder="请输入工号" maxlength="32"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="employeeForm.name" placeholder="请输入姓名" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="密码" prop="password">
              <el-input v-model="employeeForm.password" placeholder="请输入密码" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="性别" prop="gender">
              <el-select v-model="employeeForm.gender" placeholder="请选择性别">
                <el-option label="男" :value="1"></el-option>
                <el-option label="女" :value="2"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="出生日期" prop="birthDate">
              <el-date-picker
                  v-model="employeeForm.birthDate" type="date" placeholder="出生日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="联系电话" prop="contactPhone">
              <el-input v-model="employeeForm.contactPhone" placeholder="请输入联系电话" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="employeeForm.email" placeholder="请输入邮箱" maxlength="64"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="省/市/区">
              <el-cascader
                  v-model="provinceCityArea"
                  :options="provinceCityAreaOptions"
                  :props="{ expandTrigger: 'hover', value: 'code', label: 'name' }"
                  @change="handleChange"></el-cascader>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="家庭地址" prop="address">
              <el-input v-model="employeeForm.address" placeholder="请输入家庭地址" maxlength="255"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="聘用日期" prop="hireDate">
              <el-date-picker
                  v-model="employeeForm.hireDate" type="date" placeholder="聘用日期" value-format="yyyy-MM-dd">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="部门名称" prop="departmentName">
              <el-input v-model="employeeForm.departmentName" placeholder="请选择部门名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findDepartment"></i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="岗位名称" prop="positionName">
              <el-input v-model="employeeForm.positionName" placeholder="请选择岗位名称" readonly="readonly">
                <i slot="suffix" class="el-input__icon el-icon-search" @click="findPosition"></i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="pop-dialog-footer">
      <el-row>
        <el-col :span="11" :offset="6">
          <div class="form-button-container">
            <el-button type="primary" @click="onSave">保存</el-button>
            <el-button @click="onReset">重置</el-button>
            <el-button type="primary" @click="onBack">关闭</el-button>
          </div>
        </el-col>
      </el-row>
    </div>
    <el-dialog :visible.sync="enterpriseSelectorVisible" v-if="enterpriseSelectorVisible" title="企业信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <enterprise-selector @enterprise-selected-event="handleEnterpriseSelectedEvent">
      </enterprise-selector>
    </el-dialog>
    <el-dialog :visible.sync="departmentSelectorVisible" v-if="departmentSelectorVisible" title="部门信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <department-selector @department-selected-event="handleDepartmentSelectedEvent">
      </department-selector>
    </el-dialog>
    <el-dialog :visible.sync="positionSelectorVisible" v-if="positionSelectorVisible" title="岗位信息选择器(双击行选中)" :top="dialogTop" :width="dialogWidth" :append-to-body="true">
      <position-selector @position-selected-event="handlePositionSelectedEvent">
      </position-selector>
    </el-dialog>
  </div>
</template>

<script>
import employeeApi from '@/api/employeeApi'
import EnterpriseSelector from "@/views/enterprise/EnterpriseSelector.vue";
import DepartmentSelector from "@/views/department/DepartmentSelector.vue";
import PositionSelector from "@/views/position/PositionSelector.vue";
import areas from "@/locales/area.json";
export default {
  name: 'EmployeeAdd',
  components: {EnterpriseSelector,DepartmentSelector,PositionSelector,},
  props: ['id'],
  mixins: [
  ],
  data () {
    return {
      employeeForm: {
        enterpriseId: '',
        enterpriseName: '',
        employeeNumber: '',
        name: '',
        password: '',
        gender: '',
        birthDate: '',
        contactPhone: '',
        email: '',
        province: '',
        city: '',
        area: '',
        address: '',
        hireDate: '',
        departmentId: '',
        departmentName: '',
        positionId: '',
        positionName: '',
      },
      rules: {
        enterpriseName: [
          { required: true, message: '请输入企业名称', trigger: 'blur' }
        ],
        employeeNumber: [
          { required: true, message: '请输入工号', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        gender: [
          { required: true, message: '请输入性别', trigger: 'blur' }
        ],
        birthDate: [
          { required: true, message: '请输入出生日期', trigger: 'blur' }
        ],
        contactPhone: [
          { required: true, message: '请输入联系电话', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱', trigger: 'blur' }
        ],
        hireDate: [
          { required: true, message: '请输入聘用日期', trigger: 'blur' }
        ],
        departmentName: [
          { required: true, message: '请输入部门名称', trigger: 'blur' }
        ],
        positionName: [
          { required: true, message: '请输入岗位名称', trigger: 'blur' }
        ],
      },
      enterpriseSelectorVisible: false,
      departmentSelectorVisible: false,
      positionSelectorVisible: false,
      provinceCityArea: [],
      provinceCityAreaOptions: areas.provinces
    }
  },
  created () {
    if (this.id) {
      employeeApi.findById(Number(this.id))
        .then(resp => {
          if (resp && resp.code === 1) {
            this.employeeForm = resp.data
            this.provinceCityArea = []
            if (this.employeeForm.province) {
              this.provinceCityArea.push(this.employeeForm.province)
            }
            if (this.employeeForm.city) {
              this.provinceCityArea.push(this.employeeForm.city)
            }
            if (this.employeeForm.area) {
              this.provinceCityArea.push(this.employeeForm.area)
            }
          } else {
            this.$message.error('获取数据失败')
          }
        })
        .catch(error => {
          console.error('获取数据失败:', error)
        })
    }
  },
  methods: {
    onReset () {
      this.employeeForm = {
        enterpriseId: '',
        enterpriseName: '',
        employeeNumber: '',
        name: '',
        password: '',
        gender: '',
        birthDate: '',
        contactPhone: '',
        email: '',
        province: '',
        city: '',
        area: '',
        address: '',
        hireDate: '',
        departmentId: '',
        departmentName: '',
        positionId: '',
        positionName: '',
      }
      this.provinceCityArea = []
      this.$emit('reset-employee-add-event')
    },
    onSave () {
      this.$refs.refEmployeeForm.validate((valid) => {
        if (valid) {
          employeeApi.save(this.employeeForm)
            .then((resp) => {
              if (resp && resp.code === 1) {
                this.$message.success('保存成功')
                this.$emit('close-employee-add-event', {search: true})
              } else {
                this.$message.error('保存失败:' + (resp.msg ? resp.msg : ''))
              }
            })
            .catch(error => {
              console.log(error)
              this.$message.error('保存失败')
            })
        } else {
          return false
        }
      })
    },
    findEnterprise () {
      this.enterpriseSelectorVisible = true
    },
    handleEnterpriseSelectedEvent (selectedEnterprise) {
      this.enterpriseSelectorVisible = false
      if (selectedEnterprise && 'enterpriseId' in selectedEnterprise && this.employeeForm.enterpriseId !== selectedEnterprise['enterpriseId']) {
        this.employeeForm.enterpriseId = selectedEnterprise['enterpriseId']
        this.employeeForm.enterpriseName = selectedEnterprise['enterpriseName']
      }
    },
    findDepartment () {
      this.departmentSelectorVisible = true
    },
    handleDepartmentSelectedEvent (selectedDepartment) {
      this.departmentSelectorVisible = false
      if (selectedDepartment && 'departmentId' in selectedDepartment && this.employeeForm.departmentId !== selectedDepartment['departmentId']) {
        this.employeeForm.departmentId = selectedDepartment['departmentId']
        this.employeeForm.departmentName = selectedDepartment['departmentName']
      }
    },
    findPosition () {
      this.positionSelectorVisible = true
    },
    handlePositionSelectedEvent (selectedPosition) {
      this.positionSelectorVisible = false
      if (selectedPosition && 'positionId' in selectedPosition && this.employeeForm.positionId !== selectedPosition['positionId']) {
        this.employeeForm.positionId = selectedPosition['positionId']
        this.employeeForm.positionName = selectedPosition['positionName']
      }
    },
    handleChange(value) {
      if (value && value.length) {
        this.employeeForm.province = value[0]
      }
      if (value && value.length > 1) {
        this.employeeForm.city = value[1]
      }
      if (value && value.length > 2) {
        this.employeeForm.area = value[2]
      }
    },
    onBack () {
      this.$emit('close-employee-add-event')
    }
  }
}
</script>

<style lang="scss">
</style>